package com.shelpe.services.sinbad.controller.actionLog.impl.campaign;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CampaignVo;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;

@Service
public class CampaignUpdateScheduleConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertCampaign(CampaignLog entity, Map<Long, Campaign> campaignMap, List<AreaList> areas) {
		boolean campaignUpdateScheduleConverter = entity.getAction() == CampaignLog.Action.AUTO_UPDATE_SCHEDULE
				|| entity.getAction() == CampaignLog.Action.UPDATE_SCHEDULE;
		if (campaignUpdateScheduleConverter && StringUtils.isNotBlank(entity.getNote())) {
			Campaign campaign = campaignMap.get(entity.getCampaignID());
			CampaignVo vo = this.unserializeCampaign(entity.getNote());
			ActionLogDto dto = new ActionLogDto();
			dto.from(entity);
			if (campaign != null && vo != null && vo.getSchedule() != null) {
				String[] params = { campaign.getTitle(), this.from(vo.getSchedule()) };
				dto.setReason(messageSource.getMessage("message.campaign.schedule.reason", params, Locale.ENGLISH));
				return dto;
			}
		}
		return null;
	}

	// message.campaign.schedule.reason=修改分时投放：推广计划[{0}].分时投放设置：{1}

	private String from(Campaign.ScheduleObject schedule) {
		Collections.sort(schedule.getRanges(), new Comparator<Campaign.RangeObject>() {
			@Override
			public int compare(Campaign.RangeObject o1, Campaign.RangeObject o2) {
				return o1.getDayOfWeek() - o2.getDayOfWeek();
			}
		});
		StringBuffer buff = new StringBuffer();
		for (Campaign.RangeObject range : schedule.getRanges()) {
			if (range.getDayOfWeek() == 1) {
				buff.append("星期一：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
			if (range.getDayOfWeek() == 2) {
				buff.append("星期二：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
			if (range.getDayOfWeek() == 3) {
				buff.append("星期三：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
			if (range.getDayOfWeek() == 4) {
				buff.append("星期四：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
			if (range.getDayOfWeek() == 5) {
				buff.append("星期五：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
			if (range.getDayOfWeek() == 6) {
				buff.append("星期六：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
			if (range.getDayOfWeek() == 7) {
				buff.append("星期天：从");
				buff.append(range.getFrom());
				buff.append("到");
				buff.append(range.getTo());
				buff.append("折扣：");
				buff.append(range.getDiscount());
				buff.append("%;");
			}
		}
		return buff.substring(0, buff.lastIndexOf(";"));
	}
}
